package com.exploadingelephants.engine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import ch.exploadingelephants.graph.DotString;
import ch.exploadingelephants.model.Level;
import ch.exploadingelephants.model.Machine;

import com.exploadingelephants.views.DotStringView;
import com.exploadingelephants.views.MachineView;

/**
 * @author Sandro De Zanet
 * 
 */
public class DebugActivity extends Activity {

    private MachineView machineView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	int id = 100;

	Intent intent = getIntent();
	final Level level = (Level) intent
		.getSerializableExtra(MachineActivity.LEVEL_EXTRA_KEY);

	RelativeLayout layout = new RelativeLayout(this);
	layout.setGravity(Gravity.CENTER_HORIZONTAL);

	LayoutParams params = createParams();
	params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

	TextView titleView = new TextView(this);
	titleView.setText(level.getTitle());
	titleView.setId(id++);
	layout.addView(titleView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, titleView.getId());
	final Machine machine = level.getMachine();
	machineView = new MachineView(this, machine);
	machineView.setId(id++);
	machineView.setEditable(false);
	layout.addView(machineView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, machineView.getId());
	TextView descriptionView = new TextView(this);
	descriptionView.setText(level.getDescription());
	descriptionView.setId(id++);
	layout.addView(descriptionView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, descriptionView.getId());
	final TextView resultView = new TextView(this);
	resultView.setText("no result");
	resultView.setId(id++);
	layout.addView(resultView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, resultView.getId());
	final DotStringView dotStringView = new DotStringView(this,
		new DotString());
	dotStringView.setId(id++);
	layout.addView(dotStringView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, dotStringView.getId());
	Button debugButton = new Button(this);
	debugButton.setId(id++);
	debugButton.setText("debug");
	debugButton.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		machineView.simulateRobot();
	    }
	});
	layout.addView(debugButton, params);

	setContentView(layout);
    }

    private LayoutParams createParams() {
	return new RelativeLayout.LayoutParams(
		RelativeLayout.LayoutParams.WRAP_CONTENT,
		RelativeLayout.LayoutParams.WRAP_CONTENT);
    }
}